import { FVec3 } from "db://assets/fmath/FVec3";
import { SkillComponent } from "./SkillComponent";
import { SkillInstance } from "./SkillInstance";

export class SkillEntity {
    private _skillInstance: SkillInstance | null = null;
    private components: SkillComponent[] = [];
    private isActive: boolean = false;
    private _position = new FVec3();

    set skillInstance(value: SkillInstance) {
        this._skillInstance = value;
    }

    get skillInstance(): SkillInstance | null {
        return this._skillInstance;
    }

    // 添加组件
    public addComponent(component: SkillComponent): void {
        this.components.push(component);
        component.onAttach(this);
    }

    setPosition(pos: FVec3) {
        this._position.set(pos.x, pos.y, pos.z);
    }

    getPosX() {
        return this._position.x;
    }

    getPosY() {
        return this._position.y;
    }

    getPosZ() {
        return this._position.z;
    }

    getPos(out?: FVec3): FVec3 {
        if (out) {
            out.set(this._position.x, this._position.y, this._position.z);
            return out;
        } else {
            return this._position.clone();
        }
    }

    // 开始技能
    public start(): void {
        this.isActive = true;
    }

    // 更新
    public update(dt: number): void {
        if (!this.isActive) return;

        // 更新所有组件
        this.components.forEach(component => component.update(dt));

    }

    // 结束技能
    public finish(): void {
        this.isActive = false;
        this.components.forEach(component => component.onDetach());
    }

    // 打断技能
    public interrupt(): void {
        this.isActive = false;
        this.components.forEach(component => component.onInterrupt());
    }
}